<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="./js/vue.js"></script>
</head>

<body>
  <div id="app">
    <ul>
      <li v-for="item,index in carts" :key="item.id">
        <span><img :src="item.pic" alt="" style="width:50px;"></span>
        <span>{{item.name}}</span>
        <span>￥：{{item.price}}</span>
        <span>
          <button @click="decr(item)">---</button>
          <input type="number" v-model.number="item.num">
          <!-- <button @click="item.num++">+++</button> -->
          <button @click="incr(item)">+++</button>
        </span>
      </li>
    </ul>
    <div>合计：{{totalPrice()}}</div>

  </div>

  <script>

    new Vue({
      el: '#app',
      data: {
        carts: [
          { id: 1, pic: 'https://qq.yh31.com/tp/fj/202204241306036207.jpg', name: '小米10', price: 1, num: 1 },
          { id: 2, pic: 'https://qq.yh31.com/tp/fj/202204241306036207.jpg', name: '小米11', price: 1, num: 1 },
          { id: 3, pic: 'https://qq.yh31.com/tp/fj/202204241306036207.jpg', name: '小米12', price: 1, num: 1 }
        ]
      },
      methods: {
        incr(item) {
          item.num = Math.min(3, ++item.num)
        },
        decr(item) {
          item.num = Math.max(1, --item.num)
        },
        totalPrice() {
          let total = 0
          this.carts.forEach(({ num, price }) => total += num * price)
          return total
        }
      }
    })
  </script>
</body>

</html>